<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * C_Customers_Edit
 *
 * 客户编辑功能实现类
 *
 */

class C_Customers_Edit extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('System_Model');
        $this->load->model('Series_Model');
        $this->load->model('series_product_Model');
        $this->load->model('Product_Model');
        $this->load->model('Customers_Model');
    }

    /*
     * index
     * 初始化
     */

    public function index($id = FALSE) {
        $data['title'] = $this->System_Model->get_system('login_title');
        //如果为编辑，则获取该内容的详细信息
        $data['entity'] = $this->Customers_Model->GetEntity($id);

        //套系名称
        $data['selectList'] = $this->Series_Model->GetListForState(OBJ_STATE_VALID);
        //获取产品信息
        $data['productlist'] = $this->Product_Model->GetListForState(OBJ_STATE_VALID);

        $this->load->view('layout/header', $data);
        $this->load->view('layout/menu');
        $this->load->view('customers/customers_edit_view');
        $this->load->view('layout/footer');
    }

    /*
     * edit_Customers
     * 编辑数据库信息
     */

    function edit_Customers() {
        $cu_id = $this->input->post('cu_id');

        $se_item = substr($this->input->post('se_item'), 0, -1);
        if ($cu_id != NULL || $cu_id != "") {
            $data = array(
                'cu_id' => $cu_id,
                'cu_groom' => $this->input->post('cu_groom'),
                'cu_bride' => $this->input->post('cu_bride'),
                'cu_marrydate' => $this->input->post('cu_marrydate'),
                'cu_takedate' => $this->input->post('cu_takedate'),
                'cu_address' => $this->input->post('cu_address'),
                'se_id' => $this->input->post('se_id_select'),
                'cu_total' => $this->input->post('cu_total'),
                'cu_paid' => $this->input->post('cu_paid'),
                'cu_unpaid' => $this->input->post('cu_unpaid'),
                'cu_cellphone' => $this->input->post('cu_cellphone'),
                'cu_phone' => $this->input->post('cu_phone'),
                'cu_state' => OBJ_STATE_VALID
            );
            //更新
            $array = split(";", $se_item);
            $this->Customers_Model->Update_Customer_Product($cu_id, $data, $array);
        } else {
            $data = array(
                'cu_id' => $cu_id,
                'cu_groom' => $this->input->post('cu_groom'),
                'cu_bride' => $this->input->post('cu_bride'),
                'cu_marrydate' => $this->input->post('cu_marrydate'),
                'cu_takedate' => $this->input->post('cu_takedate'),
                'cu_address' => $this->input->post('cu_address'),
                'se_id' => $this->input->post('se_id_select'),
                'cu_total' => $this->input->post('cu_total'),
                'cu_paid' => $this->input->post('cu_paid'),
                'cu_unpaid' => $this->input->post('cu_unpaid'),
                'u_id' => $this->session->userdata('uid'),
                'cu_cellphone' => $this->input->post('cu_cellphone'),
                'cu_phone' => $this->input->post('cu_phone'),
                'cu_state' => OBJ_STATE_VALID
            );
            //创建
            $array = split(";", $se_item);
            $this->Customers_Model->Insert_Customer_Product($data, $array);
        }
        //跳转
        redirect('customers/C_Customers_List');
    }

    //查看
    public function show_index($id = FALSE) {
        $data['title'] = $this->System_Model->get_system('login_title');
        //如果为编辑，则获取该内容的详细信息
        $data['entity'] = $this->Customers_Model->GetEntity($id);

        //套系名称
        $data['selectList'] = $this->Series_Model->GetListForState(OBJ_STATE_VALID);

        $this->load->view('layout/header', $data);
        $this->load->view('layout/menu');
        $this->load->view('customers/customers_show_view');
        $this->load->view('layout/footer');
    }

    //异步获取套系价格和套系内容
    function ajax_Series($id) {
        if ($id) {
            $listobj = $this->series_product_Model->GetListForState(OBJ_STATE_VALID, $id);
            echo json_encode($listobj);
        }
    }

    //异步获取套系数据列表
    function ajax_GetProduct($id = FALSE) {
        if ($id) {
            $listobj = $this->ChooseContent_model->GetListForState($id);
            echo json_encode($listobj);
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */